#!/bin/bash

HIVE_HOME=/usr/bin/hive

if [ $# == 1 ]

then

   dateStr=$1

else
   dateStr=`date -d'-1 day' +'%Y-%m-%d'`
fi


echo ${dateStr}

hiveSql="
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

set hive.exec.compress.intermediate=true;
set hive.exec.compress.output=true;

set hive.exec.orc.compression.strategy=COMPRESSION;

set hive.enforce.bucketing=true;

set hive.enforce.sorting=true;

insert overwrite table dwd_zhixing04.dwd_customer_relationship partition(start_time) 
select 
    id,
    substr(ocr.create_date_time,0,4) as yearinfo,
    substr(ocr.create_date_time,6,2) as monthinfo,
    substr(ocr.create_date_time,9,2) as dayinfo,
    substr(ocr.create_date_time,12,2) as hourinfo,
    update_date_time,
    deleted,
    customer_id,
    first_id,
    belonger,
    belonger_name,
    initial_belonger,
    distribution_handler,
    business_scrm_department_id,
    last_visit_time,
    next_visit_time,
    if(
        origin_type == 'NETSERVICE' or  origin_type == 'PRESIGNUP',
        '0',
        '1'
    )as origin_type_stat,
    itcast_school_id,
    itcast_subject_id,
    intention_study_type,
    anticipat_signup_date,
    level,
    creator,
    current_creator,
    creator_name,
    origin_channel,
    comment,
    first_customer_clue_id,
    last_customer_clue_id,
    process_state,
    process_time,
    payment_state,
    substr(ocr.payment_time,0,4) as payment_year,
    substr(ocr.payment_time,6,2) as payment_month,
    substr(ocr.payment_time,9,2) as payment_day,
    substr(ocr.payment_time,12,2) as payment_hour,
    signup_state,
    signup_time,
    notice_state,
    notice_time,
    lock_state,
    lock_time,
    itcast_clazz_id,
    itcast_clazz_time,
    payment_url,
    payment_url_time,
    ems_student_id,
    delete_reason,
    deleter,
    deleter_name,
    delete_time,
    course_id,
    course_name,
    delete_comment,
    close_state,
    close_time,
    appeal_id,
    tenant,
    total_fee,
    belonged,
    belonged_time,
    belonger_time,
    transfer,
    transfer_time,
    follow_type,
    transfer_bxg_oa_account,
    transfer_bxg_belonger_name,
    '9999-99-99' as end_time,
    dt as start_time
from ods_zhixing04.ods_customer_relationship ocr where dt = '{$dateStr}' and deleted = 'false' or customer_id is not null or payment_state = 'PAID';"

${HIVE_HOME} -S -e "${hiveSql}"
